package server.util;

import java.io.PrintWriter;
import java.util.ArrayList;

import player.Player;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class BroadcasterImp implements Broadcaster {

    private ArrayList<PlayerConnection> playerConnections;

    public BroadcasterImp() {
        this.playerConnections = new ArrayList<PlayerConnection>();
    }

    @Override
    public void addConnection(Player p, PrintWriter out) {
        playerConnections.add(new PlayerConnectionNetwork(p, out));
    }
    
    @Override
    public void addConnection(PlayerConnection out) {
        playerConnections.add(out);
    }

    @Override
    public boolean broadcastToPlayer(Player p, String text) {
        for (int x = 0; x < playerConnections.size(); x++) {
            if (playerConnections.get(x).isPlayer(p.getAliveName())) {
                playerConnections.get(x).broadcastMessage(text);
                return true;
            }
        }
        return false;
    }

    @Override
    public void broadcastToAllPlayers(String text) {
        for (int x = 0; x < playerConnections.size(); x++) {
            playerConnections.get(x).broadcastMessage(text);
        }
    }

    @Override
    public void broadcastLocalToPlayer(Player p, String text) {
        for (int x = 0; x < playerConnections.size(); x++) {
            if (!playerConnections.get(x).isPlayer(p.getAliveName())) {
                playerConnections.get(x).broadcastMessageAt(p.x(), p.y(), p.z(), text);
            }
        }
    }

    @Override
    public void broadcastLocalToPlayers(Player[] p, String text) {
        for (int x = 0; x < playerConnections.size(); x++) {
            boolean playerMatch = false;
            for (int y = 0; y < p.length && !playerMatch; y++) {
                if (playerConnections.get(x).isPlayer(p[y].getAliveName())) {
                    playerMatch = true;
                }
            }
            if (!playerMatch) {
                playerConnections.get(x).broadcastMessageAt(p[0].x(), p[0].y(), p[0].z(), text);
            }
        }
    }

    @Override
    public void broadcastToRoom(int x, int y, int z, String text) {
        System.out.println("Entered broadcaster.");
        for (int c = 0; c < playerConnections.size(); c++) {
            playerConnections.get(c).broadcastMessageAt(x, y, z, text);
        }
    }

    @Override
    public void broadcastOnChannel(String channel, String text, String author) {
        for (int x = 0; x < playerConnections.size(); x++) {
            playerConnections.get(x).broadcastFromChannel(channel, text, author);
        }
    }

    @Override
    public void broadcastTickNotification() {
        for (int x = 0; x < playerConnections.size(); x++) {
            playerConnections.get(x).broadcastTickNotification();
        }
    }

    @Override
    public void broadcastServerMessage(String text) {
        for (int x = 0; x < playerConnections.size(); x++) {
            playerConnections.get(x).broadcastMessage(ColourUtil.colourise("SERVER MESSAGE: ", ColourUtil.yellow) + ColourUtil.colourise(text, ColourUtil.white));
        }
    }
}
